package com.yuchen.ishop;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.yuchen.ishop.entity.SellerXiaoer;
import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.yuchen.core.jdbc.BaseDao;
import com.yuchen.ishop.entity.Buyer;
import com.yuchen.ishop.entity.Seller;
import com.yuchen.ishop.vo.LoginParam;
import com.yuchen.jaxrs.ResponseUtils;
import com.yuchen.jaxrs.Responses;
import com.yuchen.utils.fastjson.JsonUtils;
import com.yuchen.utils.security.PasswordUtils;

/**
 *
 */
@Transactional
@Path("loginService")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class LoginServiceImpl implements  LoginService{

    private final  static Logger LOG = Logger.getLogger(LoginService.class);

    @Autowired
    private BaseDao dao;


    @POST
    @Path("buyerLogin")
    @Transactional(readOnly = true)
    public Responses buyerLogin(LoginParam loginParam) {
    	Buyer buyer = dao.fetch(Buyer.class, Cnd.where("account", "=",loginParam.getAccount()));
    	if(null == buyer)
    		return ResponseUtils.error("帐号不存在!");
    	boolean psw = PasswordUtils.verifyStr(loginParam.getPassword(),buyer.getSalt(),buyer.getPassword());
    	if(psw){
    		buyer.setPassword(null);
            Responses responses = ResponseUtils.success();
            responses.setJson(JsonUtils.toJSON(buyer));
            return responses;
        }else{
            return ResponseUtils.error("密码错误!");
        }
    }

    //路径：services/loginService/sellerLogin
    @POST
    @Path("sellerLogin")
    @Transactional(readOnly = true)
    public Responses sellerLogin(LoginParam loginParam) {
        Seller seller = dao.fetch(Seller.class, Cnd.where("account", "=",loginParam.getAccount()));
        if(null == seller)
            return ResponseUtils.error("帐号不存在!");
        boolean psw = PasswordUtils.verifyStr(loginParam.getPassword(),seller.getSalt(),seller.getPassword());
        if(psw){
            seller.setPassword(null);
            Responses responses = ResponseUtils.success();
            responses.setJson(JsonUtils.toJSON(seller));
            return responses;
        }else{
            return ResponseUtils.error("密码错误!");
        }

    }

    @POST
    @Path("xiaoerLogin")
    @Transactional(readOnly = true)
    public Responses xiaoerLogin(LoginParam loginParam) {
        SellerXiaoer sellerXiaoer = dao.fetch(SellerXiaoer.class, Cnd.where("account", "=",loginParam.getAccount()));
        if(null == sellerXiaoer)
            return ResponseUtils.error("帐号不存在!");
        boolean psw = PasswordUtils.verifyStr(loginParam.getPassword(), sellerXiaoer.getSalt(), sellerXiaoer.getPassword());
        if(psw){
            sellerXiaoer.setPassword(null);
            Responses responses = ResponseUtils.success();
            responses.setJson(JsonUtils.toJSON(sellerXiaoer));
            return responses;
        }else{
            return ResponseUtils.error("密码错误!");
        }

    }
}
